/**
 * @description  : 案件文书初始化
 * @descriptionDetail : 案件文书初始化--混入
 * @copyright    : 浙江烟草
 * @author       : mc
 * @create       : 2022-11-03 17:30:57
 */
import caseService from "@/service/case/case.service";
import officeService from "@/service/case/office.service";
import {LOG_STATUS,LOG_OP_TYPE,} from '@/service/common/mapping/enum.js';
import commonService from "@/service/common/common.service";
const caseCommon = {

  data() {
    return {
      state: {
        canEdit: undefined,
        audit: undefined,
        caseParam:{}, // 上个页面传过来的参数
      },
      docFields:[],
      showFieldTpl:false,
      fieldParam:{},
      fieldKey:"",
      justView: false,
      beginTime:"",
    }
  },
  methods: {
    docStep(cb) {
      let param = this.caseParam;
      this.justView = this.caseParam.justView;
      let routeParam = this.getRouteParam()

      if (param.id) {
        officeService.getDocIns(param.id).then(abs => {
          if (!abs.success) return;
          let res = abs.bean;
          param.type = res.step;
          this.state.canEdit = res.step === 'edit' || res.stepName == '开始';
          if(this.state.canEdit){
            this.state.canEdit = !this.justView;
          }
          this.state.audit = res.step === '01';
          if (routeParam.choseType === 'caseMap') {
            this.state.canEdit = false;
          }
          cb && cb(res);
        })
      } else {
        // 如果文书code有值，且文书uuid为空，则根据code查询一次docUuid
        if (param.docCode && !param.docUuid) {
          caseService.getDocConfig(param.docCode).then(abs => {
            if (!abs.success) return;
            let dc = abs.data;
            param.docUuid = dc.docUuid;
            this.state.canEdit = true;
            this.state.canEdit = !this.justView;
            cb && cb();
          });
        } else {
          this.state.canEdit = true;
          this.state.canEdit = !this.justView;
          cb && cb();
        }
      }
    },
    /**
	 * @param {Boolean} status 提交还是保存
	 * @param {Object} caseParam 文书参数
	 */
    saveBehaviorLog(status, caseParam, doc){

    },
    /**
	 * 解析人员
	 * 因为后台接口没有按照list的方式返回 只返回了一个字符串
	 * @param rs 文书
	 * @param idName id是那个属性
	 * @param namef name是那个属性
	 */
    parseEmployee(rs, idName, namef) {
      let namVal = namef ? rs[namef] : rs.undertaker;
      let idVal = idName ? rs[idName] : rs.undertakerUuids;
      if (!namVal || !idVal) {
        return [];
      }
      let takers = namVal.split(',');
      let uuids = idVal.split(',');
      let takerArr = [];
      for (let i = 0; i < takers.length; i++) {
        let a = takers[i];
        let vl = a.replace(')', '');
        let vls = vl.split('(');
        let no = vls[1] || '';
        no = no === 'null' || no === 'undefined' ? '' : no;
        takerArr.push({personName: vls[0], leCertNo: no, personUuid: uuids[i],});
      }
      return takerArr;
    },
    /**
	 * 弹出模板事件
	 * @param {String} key 字段名称
	 */
    tapFieldPress(key){
      if(!this.state.canEdit) return;
      this.fieldParam.bizUuid = this.caseParam.caseUuid;
      if(this.docFields && this.docFields.length > 0){
        let ob;
        this.docFields.forEach(itm => {
          if(key == itm.fieldName){
            ob = itm;
          }
        });
        if(ob != undefined){
          this.fieldParam.docUuid = ob.docUuid;
          this.fieldParam.fieldUuid = ob.fieldUuid;
          this.fieldKey = key;
          this.showFieldTpl = true;
        }
      }
    },
    /**
	 * 字段模板选择
	 */
    choiceFieldTpl(obj){
      this.$set(this[obj.formName], obj.key, obj.data.compiledContent);
      this.showFieldTpl = false;
    },
  },
}

export default caseCommon;
